var serverPort = 8085;
var server = '127.0.0.1';
var net = require('net');

console.log('connecting to server...');
var client = net.connect({server:server,port:serverPort},function(){
  	console.log('client connected');

	var request = {};
    request.magicCode = new Buffer(4);
    request.magicCode.write("\0REQ");
    request.typeCode = new Buffer(4);
    request.typeCode.write("16");//17=>ECHO_REQ
    request.size = new Buffer(4);
    request.bodyData = new Buffer("this is a test");
    request.size.write(request.bodyData.length);
  // send data
  //console.log('send data to server');
  //var body = new Buffer("greeting from client socket");
  //var size = body.length;
  //var header = new Buffer(4)
  //header.write(""+size);

  //console.log("size:"+header);
  //console.log("body:"+body);
  var clientRequest = Buffer.concat(
  	[request.magicCode,request.typeCode,request.size,request.bodyData]
  	);
  client.write(clientRequest);
});

client.on('data', function(data) {
  console.log('received data: ' + data.toString());
  client.end();
});

client.on('error',function(err){
  console.log(err);
});
client.on('end', function() {
  console.log('client disconnected');
});